const app = getApp();
const API_URL = app.globalData.API_URL;

Page({
  data: {
    customBar: app.globalData.CustomBar,
    ROOT_URL: app.globalData.ROOT_URL,
    favor: false,
    warning: null,
    Modal: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let id = options.id;
    // 获取首页轮播信息
    wx.request({
      url: API_URL + "getProductDetail",
      method: 'POST',
      data: {
        'id': id
      },
      success(res) {
        that.setData({
          product: res.data.data,
        })

        // 判断如果本人已登录，那是否已收藏此产品
        let openid = wx.getStorageSync('openid');
        if (openid) {
          that.getIsFavor(openid, res.data.data.id);
          that.seenProduct(openid, res.data.data.id);
        }
      }
    })
  },

  getIsFavor(openid, proid) {
    let that = this;
    wx.request({
      url: API_URL + "getIsFavor",
      method: 'POST',
      data: {
        'proid': proid,
        'openid': openid,
      },
      success(res) {
        if (res.data.data == 'ok') {
          that.setData({
            favor: true,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  favorProduct(e) {
    let that = this;
    let openid = wx.getStorageSync('openid');
    if (openid) {
      let proid = this.data.product.id;
      wx.request({
        url: API_URL + "favorProduct",
        method: 'POST',
        data: {
          'proid': proid,
          'openid': openid,
        },
        success(res) {
          if (res.data.data == null) {
            that.setData({
              favor: false,
            })
          } else {
            that.setData({
              favor: true,
            })
          }
        }
      })
    } else {
      console.log('请登录');
    }
  },

  seenProduct(openid, proid) {
    wx.request({
      url: API_URL + "seenProduct",
      method: 'POST',
      data: {
        'proid': proid,
        'openid': openid,
      },
      success(res) {
        console.log(res);
      }
    })
  },

  addCart() {
    let that = this;
    let proid = this.data.product.id;
    let cart = wx.getStorageSync('cart');
    if (cart && cart.length > 0) {
      let isSet = cart.filter((item)=>item.proid==proid);
      if(isSet.length>0){
        that.setData({
          warning: "此产品已添加到购物车",
          Modal: true,
        });
      }else{
        cart.push({
          'proid': proid,
          'count': 1
        });
        wx.setStorageSync('cart', cart);
        that.setData({
          warning: "添加购物车成功",
          Modal: true,
        });
      }
    }

    if (!cart || cart.length == 0) {
      let cart = [];
      cart.push({
        'proid': proid,
        'count': 1
      });
      wx.setStorageSync('cart', cart);
      that.setData({
        warning: "添加购物车成功",
        Modal: true,
      });
    }
  },

  hideModal(e) {
    this.setData({
      Modal: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})